package com.hunt.service.impl;

import com.hunt.dao.RehabilitationMapper;
import com.hunt.model.app.Rehabilitation;
import com.hunt.model.entity.*;
import com.hunt.service.KLineMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedList;
import java.util.List;


/**
 * @author sjs
 * @className
 * @description
 * @date 2020/9/16
 */

@Service
@Transactional
public class KLineMessageServiceImpl implements KLineMessageService {

    @Autowired
    private MongoTemplate stockDayMongoTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MongoTemplate stockMinKLineMongoTemplate;

    @Autowired
    private MongoTemplate indexWeekLineTemplate;

    @Autowired
    private MongoTemplate indexMonthLineTemplate;
    @Autowired
    private RehabilitationMapper rehabilitationMapper;

    @Override
    public List<MinKLine> findMinKLineDataFromMongo(String symbol, Integer start, Integer count) {

        //用来封装所有条件的对象
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC,"_id"));//发布日期排序
        query.skip(start).limit(count);
        return stockMinKLineMongoTemplate.find(query, MinKLine.class, symbol);
    }

    @Override
    public List<WeekMonthKLine> getIndexWeekLine(String s, Integer start, Integer count) {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC,"_id"));
        query.skip(start).limit(count);
        return indexWeekLineTemplate.find(query,WeekMonthKLine.class, s);
    }

    @Override
    public List<WeekMonthKLine> getIndexMonthLine(String s, Integer start, Integer count) {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC,"_id"));
        query.skip(start).limit(count);
        return indexMonthLineTemplate.find(query,WeekMonthKLine.class, s);
    }


    @Override
    public List<DayKLine> getDayKLineDataFromMongo(String symbol,Integer start,Integer count) {

        //用来封装所有条件的对象
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC,"_id"));//发布日期排序
        query.skip(start).limit(count);
        return stockDayMongoTemplate.find(query, DayKLine.class, symbol);
    }


    @Override
    public List<IndexDayKLine> getIndexDayKLineDataFromMongo(String symbol, Integer start, Integer count) {
        //用来封装所有条件的对象
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC,"_id"));//发布日期排序
        query.skip(start).limit(count);
        return stockDayMongoTemplate.find(query, IndexDayKLine.class, symbol);
    }
    @Override
    public List<Double> rehabilitationcountfront(List<Double> list,String stockCode) {
        List<Double> resultList=new LinkedList<Double>();
      //计算前复权,当天的保留位数即可
        for(Double d:list){

            resultList.add(new BigDecimal(d).setScale(4, RoundingMode.HALF_UP).doubleValue());
        }
        return resultList;
    }
    @Override
    public List<Double> rehabilitationcountback(List<Double> list,String stockCode) {
        List<Double> resultList=new LinkedList<Double>();
        List<Rehabilitation> rehabilitationList=rehabilitationMapper.getrehabilitation(stockCode);
        //计算后复权
        for(Double d:list){

            for(int i=0;i<rehabilitationList.size();i++){


                d=d*(1+rehabilitationList.get(i).getStockDividend()+rehabilitationList.get(i).getAllotment())-rehabilitationList.get(i).getAllotment()*
                        rehabilitationList.get(i).getAllotmentPrice()+rehabilitationList.get(i).getBonus();

            }
            resultList.add(new BigDecimal(d).setScale(4, RoundingMode.HALF_UP).doubleValue());
        }
        return resultList;
    }
}
